--- 模块功能：电源管理
-- @author zgt
-- @module create
-- @license MIT
-- @copyright zgt
-- @release 2022-1-6
require "net"
require "pins"
module(..., package.seeall)

local prefix = "power"
-- 设置LCD驱动电压
pmd.ldoset(15, pmd.LDO_VLCD)
pmd.ldoset(15, pmd.LDO_VMMC)
pmd.ldoset(15, pmd.LDO_VSIM1)

--local p_master = pins.setup(13, 1)
-- local p_ready =  pins.setup(4, 1)
local qc_sta={0,0,0,0,0,0,0,0,0} --继电器状态数组
local qc={} --继电器数组
qc[1] = pins.setup(11, 0)
qc[2] = pins.setup(10, 0)
qc[3] = pins.setup(12, 0)
qc[4] = pins.setup(9, 0)
qc[5] = pins.setup(5, 0) 
qc[6] = pins.setup(13, 0)
qc[7] = pins.setup(18, 0)
qc[8] = pins.setup(19, 0)
qc[9] = qc[7]
-- 数字输入
local di={}
di[1] = function()
    return 1
end
di[2]  = function()
    return 1
end
di[3] = pins.setup(24)
di[4] = pins.setup(25)
di[5] = pins.setup(26) 
di[6] = pins.setup(31) 
di[7] = pins.setup(29)
di[8] = pins.setup(30)
--蜂鸣器
BEEP = pins.setup(16, 0)
-- adc
local adc_vbat_id = 2
local adc_ir = 3

function SET_QC(i,v)
    qc[i](v)
    qc_sta[i]=v
    sys.publish("SYS_BEEP",1000)
end
function GET_QC(i)
    return qc_sta[i]
end
function GET_QC_ALL()
    local status = 0;
    for i = 1, 8 do
        status=status+(GET_QC(i) or 0)*(2^(i-1))
    end
    return status
end
function GET_DI(i)
    return di[i]()
end

-- 关闭总电源
function closeMaster()
    --p_master(0)
end
-- 锁定主电源
function openMasterLock()
    --p_master(1)
end
-- 打开输出 vbat
function openVout()
   -- p_dc_out(1)
end
function closeVout()
   -- p_dc_out(0)
end
-- 打开24V输出
function openBoost()
    --p_dc_boost(1)
end
function closeBoost()
    --p_dc_boost(0)
end
-- 打开继电器1
function openSW1()
    qc[1](1)
    qc_sta[1]=1
end
function closeSW1()
    qc[1](0)
    qc_sta[1]=0
end
function getSW1()
    return  qc_sta[1]
end
-- 打开继电器2
function openSW2()
    qc[2](1)
    qc_sta[2]=1
end
function closeSW2()
    qc[2](0)
    qc_sta[2]=0
end
function getSW2()
    return qc_sta[2]
end
-- 打开继电器3
function openSW3()
    qc[3](1)
    qc_sta[3]=1
end
function closeSW3()
    qc[3](0)
    qc_sta[3]=0
end
function getSW3()
    return qc_sta[3]
end
-- 打开继电器4
function openSW4()
    qc[4](1)
    qc_sta[4]=1
end
function closeSW4()
    qc[4](0)
    qc_sta[4]=0
end
function getSW4()
    return qc_sta[4]
end
function getFlag()
    return "111"
end
-- 电压adc通道电压读取
-- @id adc通道id 2:总电源
function getADC(id)
    adc.open(id)
    local adcValue, voltValue = adc.read(id)
    -- log.warn("power->",string.format("adc= %d,adcVal = %d,voltVal= %d",id,adcValue,voltValue))
    adc.close(id)
    if voltValue ~= 0xFFFF  then return voltValue end
    return 0
end
function getVbat()
    return getADC(adc_vbat_id) * 11 / 1000
end
function getVI()
    return getADC(adc_ir) * 11 / 1000
end
sys.taskInit(function ()
    while true do
        local res,ms = sys.waitUntil("SYS_BEEP")
        ms = ms or 100
        BEEP(1)
        sys.wait(ms)
        BEEP(0)
    end
end)
-- 判断阀门反馈
sys.taskInit(function ()
    sys.wait(2000)
    while true do
       if GET_DI(1)==0 then --阀门已开启
        SET_QC(1,0)
       end
       if GET_DI(2)==0 then --阀门已关闭
        SET_QC(2,0)
       end
       sys.wait(50)
    end
end)